package com.mofeng.myshop.controller.admin;


import com.mofeng.myshop.domain.dto.CommodityDto;
import com.mofeng.myshop.domain.dto.search.CommoditySearchDto;
import com.mofeng.myshop.domain.vo.CommodityVo;
import com.mofeng.myshop.service.ICommodityService;
import com.mofeng.myshop.until.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author MoFeng
 * @since 2024-04-14
 */
@RestController
@RequestMapping("/admin/commodity")
@RequiredArgsConstructor
@PreAuthorize(value = "@permissionService.ADMIN()")
@Tag(name = "商品的接口", description = "类介绍")
public class CommodityAdminController {
    private final ICommodityService commodityService;

    @Operation(summary = "添加商品")
    @PostMapping("")
    @PreAuthorize(value = "@permissionService.MASTER()")
    public R addCommodity(@RequestBody CommodityDto commodityDto) {
        return commodityService.saveCommodity(commodityDto);
    }

    @Operation(summary = "删除商品")
    @DeleteMapping("")
    @PreAuthorize(value = "@permissionService.MASTER()")
    public R delCommodity(@RequestParam("id") List<String> ids) {
        return commodityService.delCommodity(ids);
    }

    @Operation(summary = "修改商品")
    @PutMapping("")
    @PreAuthorize(value = "@permissionService.MASTER()")
    public R updateCommodity(@RequestBody CommodityDto commodityDto) {
        return commodityService.saveCommodity(commodityDto);
    }

    @Operation(summary = "获取商品列表")
    @GetMapping("/list/{page}/{size}")
    public R getCommodityListPage(@PathVariable("page") int page,
                                  @PathVariable("size") int size,
                                  @RequestParam(value = "categoryId", required = false) String categoryId,
                                  @RequestParam(value = "projectId", required = false) String projectId,
                                  @RequestParam(value = "name", required = false) String name) {
        CommoditySearchDto commoditySearch = new CommoditySearchDto();
        commoditySearch.setPage(page);
        commoditySearch.setSize(size);
        commoditySearch.setKeyword(name);
        commoditySearch.setCommodityCategoryId(categoryId);
        commoditySearch.setProjectId(projectId);
        return commodityService.getCommodityByPageAdmin(commoditySearch);
    }
}
